package com.example.cinemaorder.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 高垚淼
 * @version 1.0
 */
@Configuration
public class RabbitmqConfig {

//    支持传输对象
    @Bean
    public Jackson2JsonMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //---电影排片查询---
    //声明队列，电影排片时间查询，使用rabbitmq减轻服务器压力
    @Bean
    public Queue movieSearch(){
        return new Queue("movieSearch");
    }

    //topisc模式使用topic类型的交换机
    @Bean
    public TopicExchange exchangetopicMovieSearch(){
        return new TopicExchange("exchangetopicMovieSearch");
    }

    //绑定交换机和路由
    @Bean
    public Binding bindingqueueemailtoexchangetopic(@Qualifier("movieSearch")Queue movieSearch, TopicExchange exchangetopicMovieSearch){
        return BindingBuilder.bind(movieSearch).to(exchangetopicMovieSearch).with("movieSearch");
    }

//---下单---
    //削峰下单高峰期
    @Bean
    public Queue getOrder(){
        return new Queue("getOrder");
    }

    //topisc模式使用topic类型的交换机
    @Bean
    public TopicExchange exchangetopicGetOrder(){
        return new TopicExchange("exchangetopicGetOrder");
    }

    //绑定交换机和路由
    @Bean
    public Binding bindingqueueemailtoexchangetopic2(@Qualifier("getOrder")Queue getOrder, TopicExchange exchangetopicGetOrder){
        return BindingBuilder.bind(getOrder).to(exchangetopicGetOrder).with("#.getOrder.#");
    }
    //添加死信+延迟队列，订单倒计时内支付
    //延迟队列，如果15分钟内未支付，投递到死信队列处理
    @Bean
    public Queue getOrderDead(){
        Map<String, Object> arguments=new HashMap<>();
        //设置消息的等待时间
        arguments.put("x-message-ttl",20000);
        //变成死信以后，进入哪个交换机
        arguments.put("x-dead-letter-exchange","exchangetopicGetOrderDeadDeal");
        //进入死信交换机以后，通过指定的路由，进入死信队列（保持和死信队列的建键一致）
        arguments.put("x-dead-letter-routing-key","getOrderDeadDeal");
        return new Queue("getOrderDead",true,false,false,arguments);
    }

    //绑定交换机和路由
    @Bean
    public Binding bindingqueueemailtoexchangetopic3(@Qualifier("getOrderDead")Queue getOrderDead, TopicExchange exchangetopicGetOrder){
        return BindingBuilder.bind(getOrderDead).to(exchangetopicGetOrder).with("#.getOrderDead.#");
    }
    //定义死信交换机，修改未支付订单状态
    @Bean
    public Queue getOrderDeadDeal(){
        return new Queue("getOrderDeadDeal");
    }

    @Bean
    public TopicExchange exchangetopicGetOrderDeadDeal(){
        return new TopicExchange("exchangetopicGetOrderDeadDeal");
    }

    @Bean
    public Binding bindingqueueorderdlxtoexchangeorderdlx(Queue getOrderDeadDeal, TopicExchange exchangetopicGetOrderDeadDeal){
        return BindingBuilder.bind(getOrderDeadDeal).to(exchangetopicGetOrderDeadDeal).with("#.getOrderDeadDeal.#");
    }
}
